package com.test;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class Start {
    public volatile static boolean flag = true;
    private int startThread;
    private int endThread;
    private int sleep1;
    private int sleep2;
    private String location;
    private int oneThreadBufferSize;
    public static volatile AtomicInteger atomicInteger = new AtomicInteger(0);
    private volatile LinkedBlockingQueue<BufferedImage> imgQuque;

    public static void main(String[] args) {
        Start start1 = new Start();
        start1.init();
        start1.run();
    }

    public void init() {
        Properties properties = new Properties();
        InputStream resourceAsStream = Start.class.getClassLoader().getResourceAsStream("config.properties");
        if (resourceAsStream != null) {
            try {
                properties.load(resourceAsStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        sleep1 = Util.getIntProperties(properties, "sleep1");
        sleep2 = Util.getIntProperties(properties, "sleep2");
        startThread = Util.getIntProperties(properties, "startThread");
        endThread = Util.getIntProperties(properties, "endThread");
        location = Util.getStrProperties(properties, "location");
        oneThreadBufferSize = Util.getIntProperties(properties, "bufferQueueSize");
    }

    public void run() {
        for (int i = startThread; i < endThread; i++) {
            System.out.println(new Date() + " thread num = " + i + " start");
            imgQuque = new LinkedBlockingQueue<>(i * oneThreadBufferSize);
            for (int i1 = 0; i1 < i; i1++) {
                new Thread(new Task(location, atomicInteger, imgQuque), i + "->" + (i1 + 1)).start();
            }
            Util.sleep(sleep1 * 1000);
            flag = false;
            System.out.println(new Date() + " thread num = " + i + " finish" + " create file num = " + atomicInteger.get() + " imageQueueSize = " + imgQuque.size());
            Util.sleep(sleep2 * 1000);
            flag = true;
        }
    }
}
